package com.extrace.loader;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;

import com.extrace.misc.model.TransNode;
import com.extrace.net.HttpAsyncTask;
import com.extrace.net.HttpResponseParam;
import com.extrace.net.IDataAdapter;
import com.extrace.net.JsonUtils;
import com.extrace.ui.main.ExTraceApplication;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class TransNodeListLoader extends HttpAsyncTask {

    String url;
    IDataAdapter<List<TransNode>> adapter;
    private Activity context;

    public TransNodeListLoader(IDataAdapter<List<TransNode>> adapter, Activity context) {
        super(context);
        this.context = context;
        this.adapter = adapter;
        url = ((ExTraceApplication)context.getApplication()).getMiscServiceUrl();
    }

    @Override
    public void onDataReceive(String class_name, String json_data) {
        if(json_data == null || json_data.length() == 0)
        {
            Toast.makeText(context, "没有符合条件的网点信息!", Toast.LENGTH_SHORT).show();
            adapter.setData(null);
        }
        else
        {
            List<TransNode> transNodes = JsonUtils.fromJson(json_data, new TypeToken<List<TransNode>>(){});
            adapter.setData(transNodes);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onStatusNotify(HttpResponseParam.RETURN_STATUS status, String str_response) {
        Log.i("onStatusNotify", "onStatusNotify: " + str_response);
    }

    public void LoadTransNodeAll() {
        url += "getNodeAll";
        try {
            execute(url, "GET");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
